package com.example.opengldemo.simple12_03;
import com.example.opengldemo.util.MatrixState;

import static com.example.opengldemo.simple12_03.Constant.*;
public class BallForControl
{	
	public static final float TIME_SPAN=0.05f;
	public static final float G=0.8f;
	
	BallTextureByVertex btv;
	float startY;
	float timeLive=0;
	float currentY=0;
	float vy=0;
	
	public BallForControl(BallTextureByVertex btv,float startYIn)
	{
		this.btv=btv;
		this.startY=startYIn;		
		currentY=startYIn;
		new Thread()
		{//����һ���߳��˶�����
			public void run()
			{
				while(true)
				{

					timeLive+=TIME_SPAN;
					float tempCurrY=startY-0.5f*G*timeLive*timeLive+vy*timeLive;
					
					
					if(tempCurrY<=0)
					{

						startY=0;		

						vy=-(vy-G*timeLive)*0.995f;

						timeLive=0;

						if(vy<0.35f)
						{
							currentY=0;
							break;
						}
					}
					else
					{
						currentY=tempCurrY;
					}
					
					try
					{
						Thread.sleep(20);
					}
					catch(Exception e)
					{
						e.printStackTrace();
					}
				}
			}
		}.start();
	}
	
	public void drawSelf(int texId)
	{
		MatrixState.pushMatrix();
		MatrixState.translate(0, UNIT_SIZE*BALL_SCALE+currentY, 0);
		btv.drawSelf(texId);		
		MatrixState.popMatrix();
	}
	
	public void drawSelfMirror(int texId)
	{
		MatrixState.pushMatrix();
		MatrixState.translate(0, -UNIT_SIZE*BALL_SCALE-currentY, 0);
		btv.drawSelf(texId);		
		MatrixState.popMatrix();
	}
}
